<?php
  $track_period = time()-(20*60)+$this->config['server_offset_time'];
?>
<section class="content-header">
    <h1>
        <?php echo $this->_lang["visitor_online"]; ?>
        <small>Welcome</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="./"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li class="active"><i class="fa fa-group"></i> <?php echo $this->_lang["visitor_online"]; ?></li>
    </ol>
</section>

<!-- Main content -->
<section class="content">
	<div class="box box-primary">
        <div class="box-header">
            <i class="fa fa-group"></i>
            <h3 class="box-title"><?php echo $this->_lang["visitor_online"]; ?></h3>
        </div>
        <div class="box-body">
        	  <p>
			  <?php printf($this->_lang["visitor_online_message"], strftime($this->config['time_format'], $track_period)); ?>
              </p>
            
              <table class="display" id="online-tbl">
                <thead>
                  <tr>
                    <th>&nbsp;</th>
                    <th><?php echo $this->_lang['document']; ?></th>
                    <th><?php echo $this->_lang['hostname']; ?></th>
                    <th><?php echo $this->_lang['ua']; ?></th>
                    <th><?php echo $this->_lang['os']; ?></th>
                  </tr>
                  </thead>
            <?php
              // get page titles
              $sql = "SELECT id, pagetitle FROM ".$this->db."site_content";
              $rs = $this->dbQuery($sql);
              $pagetitles = array();
              while ($row = $this->fetchRow($rs)) {
                $pagetitles[$row['id']] = $row['pagetitle'];
              }
            
              $sql = "SELECT DISTINCT(visitor) AS visitor, MAX(timestamp) AS lasthit FROM ".$this->db."log_access WHERE timestamp > $track_period GROUP BY visitor ORDER BY lasthit DESC";
              $rs = $this->dbQuery($sql);
              $limit = $this->recordCount($rs);
              for($i=0; $i<$limit; $i++) {
                $tmp = $this->fetchRow($rs);
                $visitor = $tmp['visitor'];
            
                $sql = "SELECT document, referer, timestamp AS lasthit FROM ".$this->db."log_access WHERE timestamp > $track_period AND visitor=$visitor ORDER BY timestamp DESC LIMIT 1";
                $rs2 = $this->dbQuery($sql);
                $tmp2 = $this->fetchRow($rs2);
                $document = $tmp2['document'];
                $lasthit = $tmp2['lasthit'];
                $referer = $tmp2['referer'];
            
                $sql = "SELECT t1.data AS ua FROM ".$this->db."log_user_agents AS t1, ".$this->db."log_visitors AS t2 WHERE t2.id=$visitor AND t1.id=t2.ua_id";
                $rs2 = $this->dbQuery($sql);
                $tmp2 = $this->fetchRow($rs2);
                $ua = $tmp2['ua'];
            
                $sql = "SELECT t1.data AS os FROM ".$this->db."log_operating_systems AS t1, ".$this->db."log_visitors AS t2 WHERE t2.id=$visitor AND t1.id=t2.os_id";
                $rs2 = $this->dbQuery($sql);
                $tmp2 = $this->fetchRow($rs2);
                $os = $tmp2['os'];
            
                $sql = "SELECT t1.data AS hostname FROM ".$this->db."log_hosts AS t1, ".$this->db."log_visitors AS t2 WHERE t2.id=$visitor AND t1.id=t2.host_id";
                $rs2 = $this->dbQuery($sql);
                $tmp2 = $this->fetchRow($rs2);
                $host = $tmp2['hostname'];
            
                $sql = "SELECT data AS referer FROM ".$this->db."log_referers WHERE id=$referer";
                $rs2 = $this->dbQuery($sql);
                $tmp2 = $this->fetchRow($rs2);
                $referer = $tmp2['referer'];
                $refererString = $referer!='Internal' && $referer!='Unknown' ? "<span style='font-size:9px;'>".$this->_lang['referrer']." "."<a href='$referer' target='_blank' style='font-size:9px;'>$referer</a></span>" : " ";
            ?>
                <tr>
                  <td class='row3'><b><?php echo strftime($this->config['time_format'], $lasthit) ?></b></td>
                  <td class='row1'><?php echo $pagetitles[$document]; ?></td>
                  <td class='row1'><?php echo $host; ?></td>
                  <td class='row1'><?php echo $ua; ?></td>
                  <td class='row1'><?php echo $os; ?></td>
                </tr>
                <!-- <tr>
                  <td class='row1' colspan="5" align="right"><?php echo $refererString; ?></td>
                </tr> -->
            <?php
              }
              if($limit==0) {
            ?>
                <!-- <tr>
                  <td class='row1' colspan="5"><?php echo $this->_lang['no_online_users']; ?></td>
                </tr> -->
            <?php
              }
            ?>
            </table>
            <script type="text/javascript">
				roTable = $('#online-tbl').dataTable({
						"bJQueryUI": true,
						"sPaginationType": "full_numbers"
					});
			</script>
        </div>
    </div>
</section>